

package com.KOL.present.game;

import com.KOL.model.character.Classification;
import com.KOL.model.character.PlayerCharacter;
import com.KOL.model.character.Race;
import com.KOL.model.character.Sex;
import com.KOL.present.ChunkDisplay;
import com.util.ImagesLoader;

import java.util.Vector;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

public class CreationWindow extends JPanel implements ActionListener, Runnable {
    private static final int PWIDTH = 1280; //TODO: pull from config
    private static final int PHEIGHT = 720; //TODO: pull from config
    private static final int PLAYER_X = 12;
    private static final int PLAYER_Y = 7;

    private Vector<String> sexChoices;
    private Vector<String> raceChoices;
    private Vector<String> classChoices;
    
    private JButton btnCreate;
    private JButton btnExit;

    private JLabel lblName;
    private JLabel lblSex;
    private JLabel lblRace;
    private JLabel lblClass;
    private JLabel lblBio;

    private JScrollPane spBio;
    private JTextArea txtBio;
    private JTextField txtName;
    private JComboBox cbRace;
    private JComboBox cbClass;
    private JComboBox cbSex;

    private JPanel upperPanel;
    private JPanel lowerPanel;
    private JSplitPane spltMainPane;

    private boolean heroCreated;

    private GameWindow window;

    public CreationWindow(GameWindow window) {
        super();

        this.window = window;
        upperPanel = new JPanel(new FlowLayout());
        lowerPanel = new JPanel(new FlowLayout());

        btnCreate = new JButton("Create");
        btnCreate.addActionListener(this);
        btnExit = new JButton("Exit");
        btnExit.addActionListener(this);

        lblName = new JLabel("Hero Name: ");
        lblSex = new JLabel("Sex: ");
        lblRace = new JLabel("Race: ");
        lblClass = new JLabel("Class: ");
        lblBio = new JLabel("Bio: ");

        txtBio = new JTextArea(5,20);
        spBio = new JScrollPane(txtBio);
        spBio.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        txtName = new JTextField(20);

        loadChoices();

        cbRace = new JComboBox(raceChoices.toArray());
        cbSex = new JComboBox(sexChoices.toArray());
        cbClass = new JComboBox(classChoices.toArray());

        cbRace.addActionListener(this);

        upperPanel.add(lblName);
        upperPanel.add(txtName);
        upperPanel.add(lblSex);
        upperPanel.add(cbSex);
        upperPanel.add(lblRace);
        upperPanel.add(cbRace);
        upperPanel.add(lblClass);
        upperPanel.add(cbClass);

        lowerPanel.add(lblBio);
        lowerPanel.add(spBio);
        lowerPanel.add(btnExit);
        lowerPanel.add(btnCreate);

        spltMainPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,upperPanel,lowerPanel);
        spltMainPane.setEnabled(false);

        add(spltMainPane, "Center");

        heroCreated = false;
    }

    public void loadChoices() {
        sexChoices = new Vector<String>();
        raceChoices = new Vector<String>();
        classChoices = new Vector<String>();

        sexChoices.add("");
        for (Sex sex : Sex.values())
            sexChoices.add(sex.toString());

        raceChoices.add("");
        for (Race race : Race.values())
            raceChoices.add(race.toString());

    }

    public PlayerCharacter getPlayer(ImagesLoader imsLoader, ChunkDisplay chunk) {
        PlayerCharacter player;
        setupWindow();
        window.addPanel(this);
    
        run();

        String name = txtName.getText();
        String bio = txtBio.getText();
        Sex sex = Sex.valueOf(cbSex.getSelectedItem().toString());
        Race race = Race.valueOf(cbRace.getSelectedItem().toString());
        Classification classification = Classification.valueOf(cbClass.getSelectedItem().toString());

        player = new PlayerCharacter(name,bio,sex,classification, race, PLAYER_X, PLAYER_Y, PWIDTH, PHEIGHT, imsLoader,chunk);

        window.remove(this);
        return player;
    }


    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(cbRace)) {
            upperPanel.remove(cbClass);

            if(cbRace.getSelectedItem().equals("")) {
                cbClass = new JComboBox();
            } else {
                cbClass = new JComboBox(Classification.getAvailableFor(Race.valueOf((String)cbRace.getSelectedItem())).toArray());
            }
            upperPanel.add(cbClass);
            window.pack();
            doLayout();
        }else if(e.getSource().equals(btnCreate)) {
            if(txtName.getText().isEmpty())
                JOptionPane.showMessageDialog(null, "Required: Name", "Required", JOptionPane.ERROR_MESSAGE);
            else if(cbRace.getSelectedItem().equals(""))
                JOptionPane.showMessageDialog(null, "Required: Race", "Required", JOptionPane.ERROR_MESSAGE);
            else if(cbSex.getSelectedItem().equals(""))
                JOptionPane.showMessageDialog(null, "Required: Sex", "Required", JOptionPane.ERROR_MESSAGE);
            else if(cbClass.getSelectedItem() == null)
                JOptionPane.showMessageDialog(null, "Required: Class", "Required", JOptionPane.ERROR_MESSAGE);
            else
                heroCreated = true;

        }else if(e.getSource().equals(btnExit))
            System.exit(0);
    }

    public void setupWindow() {
        setDoubleBuffered(false);
        setBackground(Color.black);
        setPreferredSize( new Dimension(PWIDTH, PHEIGHT));
        setVisible(true);
    }

    public void run() {
        try {
            while(!heroCreated)
                Thread.sleep(500L);
        } catch(InterruptedException ex){}
    }
}
